package io.wongxd.solution.baseView

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.compose.runtime.*
import androidx.compose.ui.platform.ComposeView
import io.wongxd.solution.compose.composeTheme.AppTheme
import io.wongxd.solution.compose.composeTheme.AppThemeHolder
import kotlinx.coroutines.MainScope

abstract class BaseComposeFragment : BaseRootFragmentLast() {

    override val layoutId: Int
        get() = 0

    override val autoSetBackgroundColor: Boolean
        get() = false

    override val needSetStatusBarByBase: Boolean
        get() = false

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.setBackgroundColor(AppThemeHolder.appBgColorForView)
    }

    protected var rootView: ComposeView? = null

    final override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        rootView = ComposeView(requireContext()).apply {
            setContent {
                SetComposeRoot()
            }
        }
        return rootView
    }

    @Composable
    protected open fun SetComposeRoot() {
        InjectTheme {
            Render()
        }
    }

    @Composable
    protected open fun InjectTheme(content: @Composable () -> Unit) {
        AppTheme {
            content()
        }
    }

    protected open fun reRender() {
        rootView?.setContent { SetComposeRoot() }
    }

    @Composable
    open fun Render() {

    }


    protected val mainScope = MainScope()

    protected fun String.showToast() {
        Toast.makeText(requireContext(), this, Toast.LENGTH_SHORT).show()
    }
}